[...path].ts

 1import fs from 'node:fs';
 2import path from 'node:path';
 3
 4const ROOT = process.cwd();
 5const FIXTURES_DIR = path.join(ROOT, 'tests', 'fixtures');
 6
 7function walk(dir: string): string[] {
 8  const out: string[] = [];
 9  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
10    const fullPath = path.join(dir, entry.name);
11    if (entry.isDirectory()) out.push(...walk(fullPath));
12    else if (entry.name.endsWith('.html')) out.push(fullPath);
13  }
14  return out;
15}
16
17export function getStaticPaths() {
18  return walk(FIXTURES_DIR).map((filePath) => ({
19    params: {
20      path: path.relative(FIXTURES_DIR, filePath).replace(/\.html$/, '').split(path.sep).join('/'),
21    },
22    props: {
23      filePath,
24    },
25  }));
26}
27
28function withDetectorScript(html: string) {
29  if (html.includes('detect-antipatterns-browser.js')) return html;
30  const script = [
31    '<script>',
32    'window.__IMPECCABLE_CONFIG__ = { ...(window.__IMPECCABLE_CONFIG__ || {}), autoScan: true };',
33    '</script>',
34    '<script src="/js/detect-antipatterns-browser.js"></script>',
35  ].join('');
36  if (/<\/body>/i.test(html)) return html.replace(/<\/body>/i, `${script}</body>`);
37  return `${html}${script}`;
38}
39
40export function GET({ props }: { props: { filePath: string } }) {
41  return new Response(withDetectorScript(fs.readFileSync(props.filePath, 'utf-8')), {
42    headers: {
43      'Content-Type': 'text/html; charset=utf-8',
44      'Cache-Control': 'no-store',
45    },
46  });
47}